﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.TextScraping
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class PdfSectionFilterInstruction:InstructionBase
    {
        #region props
        private List<string> _HeaderPatterns;
        /// <summary></summary>
        public List<string> HeaderPatterns
        {
            get
            {
                return this._HeaderPatterns;
            }
            set
            {
                this._HeaderPatterns = value;
            }
        }

        private List<string> _FooterPatterns;
        /// <summary></summary>
        public List<string> FooterPatterns
        {
            get
            {
                return this._FooterPatterns;
            }
            set
            {
                this._FooterPatterns = value;
            }
        }

        private bool _AcrossMultiPages;
        /// <summary></summary>
        public bool AcrossMultiPages
        {
            get
            {
                return this._AcrossMultiPages;
            }
            set
            {
                this._AcrossMultiPages = value;
            }
        }

        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._ConnStr;
            }
            set
            {
                this._ConnStr = value;
            }
        }

        private string _SelectSql;
        /// <summary></summary>
        public string SelectSql
        {
            get
            {
                return this._SelectSql;
            }
            set
            {
                this._SelectSql = value;
            }
        }

        private string _IDField;
        /// <summary></summary>
        public string IDField
        {
            get
            {
                return this._IDField;
            }
            set
            {
                this._IDField = value;
            }
        }

        private string _FilePathField;
        /// <summary></summary>
        public string FilePathField
        {
            get
            {
                return this._FilePathField;
            }
            set
            {
                this._FilePathField = value;
            }
        }

        private string _TgtConnStr;
        /// <summary></summary>
        public string TgtConnStr
        {
            get
            {
                return this._TgtConnStr;
            }
            set
            {
                this._TgtConnStr = value;
            }
        }

        private string _TgtTableName;
        /// <summary></summary>
        public string TgtTableName
        {
            get
            {
                return this._TgtTableName;
            }
            set
            {
                this._TgtTableName = value;
            }
        }

        private string _TgtIDField;
        /// <summary></summary>
        public string TgtIDField
        {
            get
            {
                return this._TgtIDField;
            }
            set
            {
                this._TgtIDField = value;
            }
        }

        private string _TgtContentField;
        /// <summary></summary>
        public string TgtContentField
        {
            get
            {
                return this._TgtContentField;
            }
            set
            {
                this._TgtContentField = value;
            }
        }

        private string _FlagField;
        /// <summary></summary>
        public string FlagField
        {
            get
            {
                return this._FlagField;
            }
            set
            {
                this._FlagField = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public PdfSectionFilterInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._ConnStr = string.Empty;
            this._SelectSql = string.Empty;
            this._IDField = string.Empty;
            this._FilePathField = string.Empty;
            this._HeaderPatterns = new List<string>();
            this._FooterPatterns = new List<string>();
            this._AcrossMultiPages = false;
            this._TgtConnStr = string.Empty;
            this._TgtTableName = string.Empty;
            this._TgtIDField = string.Empty;
            this._TgtContentField = string.Empty;
            this._FlagField = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="connStr"></param>
        /// <param name="selectSql"></param>
        /// <param name="idField"></param>
        /// <param name="filePathField"></param>
        /// <param name="tgtConnStr"></param>
        /// <param name="tgtTblName"></param>
        /// <param name="tgtIDField"></param>
        /// <param name="contentField"></param>
        /// <param name="flagField"></param>
        /// <param name="headerPatterns"></param>
        /// <param name="footerPatterns"></param>
        /// <param name="acrossPages"></param>
        public PdfSectionFilterInstruction(Guid componentID, Guid jobTicketID,
            string connStr, string selectSql, string idField,string filePathField,
            string tgtConnStr, string tgtTblName, string tgtIDField, 
            string contentField, string flagField,
            List<string> headerPatterns, List<string> footerPatterns, bool acrossPages) 
            : base(componentID, jobTicketID)
        {
            this._ConnStr = connStr;
            this._SelectSql = selectSql;
            this._IDField = idField;
            this._FilePathField = filePathField;
            this._TgtConnStr = tgtConnStr;
            this._TgtTableName = tgtTblName;
            this._TgtIDField = tgtIDField;
            this._TgtContentField = contentField;
            this._FlagField = flagField;
            this._HeaderPatterns = headerPatterns;
            this._FooterPatterns = footerPatterns;
            this._AcrossMultiPages = acrossPages;
        }
        #endregion

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"ConnStr",this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SelectSql", this.SelectSql);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IDField", this.IDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilePathField", this.FilePathField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtConnStr", this.TgtConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtTableName", this.TgtTableName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtIDField", this.TgtIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtContentField", this.TgtContentField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FlagField",this.FlagField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "AcrossPages", this.AcrossMultiPages.ToString());

            if(this.HeaderPatterns !=null && this.HeaderPatterns.Count>0)
            {
                XmlNode headerRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "HeaderPatterns");
                foreach(string headerPattern in this.HeaderPatterns)
                {
                    XmlNode headerNode = XmlDataUtil.AddElement(ref xDoc, headerRootNode, "HeaderPattern");
                    XmlDataUtil.UpdateAttribute(ref xDoc, headerNode, "Pattern",headerPattern);
                }
            }
            if(this.FooterPatterns !=null && this.FooterPatterns.Count>0)
            {
                XmlNode footerRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "FooterPatterns");
                foreach(string footerPattern in this.FooterPatterns)
                {
                    XmlNode footerNode = XmlDataUtil.AddElement(ref xDoc, footerRootNode, "FooterPattern");
                    XmlDataUtil.UpdateAttribute(ref xDoc, footerNode, "Pattern",footerPattern);
                }
            }

            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                PdfSectionFilterInstruction instruction = new PdfSectionFilterInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                instruction.SelectSql = XmlDataUtil.GetAttributeValue(xNode, "SelectSql", "");
                instruction.FilePathField = XmlDataUtil.GetAttributeValue(xNode, "FilePathField", "");
                instruction.IDField = XmlDataUtil.GetAttributeValue(xNode, "IDField", "");
                instruction.TgtConnStr = XmlDataUtil.GetAttributeValue(xNode, "TgtConnStr", "");
                instruction.TgtTableName = XmlDataUtil.GetAttributeValue(xNode, "TgtTable", "");
                instruction.TgtIDField = XmlDataUtil.GetAttributeValue(xNode, "TgtIDField", "");
                instruction.TgtContentField = XmlDataUtil.GetAttributeValue(xNode, "TgtContentField", "");
                instruction.FlagField = XmlDataUtil.GetAttributeValue(xNode, "FlagField", "");

                XmlNodeList headerNodes = xNode.SelectNodes("HeaderPatterns/HeaderPattern");
                if(headerNodes !=null && headerNodes.Count>0)
                {
                    instruction.HeaderPatterns=new List<string>();
                    foreach(XmlNode headerNode in headerNodes)
                    {
                        instruction.HeaderPatterns.Add(XmlDataUtil.GetAttributeValue(headerNode,"Pattern",""));
                    }
                }
                XmlNodeList footerNodes = xNode.SelectNodes("FooterPatterns/FooterPattern");
                if(footerNodes !=null && footerNodes.Count>0)
                {
                    instruction.FooterPatterns=new List<string>();
                    foreach(XmlNode footerNode in footerNodes)
                    {
                        instruction.FooterPatterns.Add(XmlDataUtil.GetAttributeValue(footerNode, "Pattern", ""));
                    }
                }

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }
    }
}
